package model;


import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Vps;

public class VpsDAO {
    
    /**
     * Load Vps in Shop.
     * @return List Vps
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public static ArrayList<Vps> getList() throws Exception {
        ArrayList<Vps> list = new ArrayList<Vps>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Vps>) session.createCriteria(Vps.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Vps
     * @param Vps
     * @throws Exception
     */
    public static void add(Vps vps) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(vps);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Vps
     * @param Vps
     * @throws Exception
     */
    public static void update(Vps vps) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(vps);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Vps
     * @param Vps
     * @throws Exception
     */
    public static void delete(Vps vps) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(vps);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Vps by ID
     * @param id
     * @return Vps
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Vps find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Vps vps = new Vps();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            vps = (Vps) session.get(Vps.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return vps;
        }
    }
}
